package com.hxk.behaviorMode.Interpreter;

import java.util.Map;
import java.util.Stack;

public class Calculator {
	
	private Expression expression;
	
	public Calculator(String expStr){
		Stack<Expression> stack = new Stack<Expression>();
		char[] charArray = expStr.toCharArray();
		Expression left = null;
		Expression right = null;
		for(int i = 0; i < charArray.length; i++){
			System.out.println(i);
			System.out.println(">>>>"+charArray[i]);
			switch (charArray[i]) {
			case '+':
				left = stack.pop();
				char ss = charArray[++i];
				System.out.println(String.valueOf("==>>>>>"+ss));
				right = new VarExpression(String.valueOf(ss));
				stack.push(new AddExpression(left, right));
				break;
			case '-':
				left = stack.pop();
				char sss = charArray[++i];
				System.out.println(String.valueOf("==>>>>>"+sss));
				right = new VarExpression(String.valueOf(sss));
				stack.push(new SubExpression(left, right));
				break;
			default:
				stack.push(new VarExpression(String.valueOf(charArray[i])));
			}
			System.out.println(i);
		}
		System.out.println(stack.size());
		this.expression = stack.pop();
		System.out.println(this.expression);
	}
	
	public int run(Map<String, Integer> var){
		return this.expression.interpreter(var);
	}

}
